﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.IO;

namespace xEngine.Utils
{
    public class LocaleParser
    {
        #region Singleton

        private static LocaleParser inst = new LocaleParser();
        private LocaleParser()
        {
            _strings = new Dictionary<String, String>();
        }

        #endregion

        #region Member Variables

        private Dictionary<String, String> _strings;

        #endregion

        #region Properties

        public static bool IsInitialized { get { return inst._strings.Count > 0; } }

        #endregion

        #region Functions

        public static void Initialize(String path, String lang)
        {
            inst._strings.Clear();

            try
            {
                using (XmlReader reader = XmlReader.Create(DataLoader.GetStream(Path.Combine(path, lang, "strings.xml"))))
                {
                    while (reader.Read())
                        if (reader.NodeType == XmlNodeType.Element && reader.Name.ToLower() == "string")
                        {
                            inst._strings.Add(reader.GetAttribute("name"), reader.ReadString());
                        }
                }
            }
            catch (Exception e)
            {
                xEngine.Debugging.LogWriter.WriteLine = e.Message;
            }
        }

        public static String GetString(String name)
        {
            return IsInitialized ? inst._strings[name] : "Not Initialized!";
        }
        
        #endregion
    }
}
